package com.jiangli.controller.user;

import com.jiangli.annotation.RateLimit;
import com.jiangli.entity.Dish;
import com.jiangli.mapper.DishMapper;
import com.jiangli.result.Result;
import com.jiangli.service.DishService;
import com.jiangli.service.UserPreferenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;

@RestController
@RequestMapping("/user/actions")
public class UserActionController {
    @Autowired
    private UserPreferenceService preferenceService;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishService dishService;

    /**
     * 记录用户行为（点击、收藏、购买）
     */
    @PostMapping
    @RateLimit
    public Result<String> recordAction(
            @RequestParam Long userId,
            @RequestParam Long dishId,
            @RequestParam String action) { // action: click, collect, buy
        //TODO 将其改为用封装好的类
        Dish dish = dishService.getDishesFromCacheOrDB(Collections.singletonList(dishId)).get(0);
        preferenceService.recordUserAction(userId, dish, action);
        return Result.success("成功");
    }
}
